<!DOCTYPE html>
<html>
<head>
    <title>Touch Adjustment : Adjust correctly for scroll offset - bug 86205</title>
    <script src="../resources/js-test.js"></script>
    <script src="resources/touchadjustment.js"></script>
    <style>
        #scrolloffsetcontainer {
            position: absolute;
            top: 500px;
            height: 1000px;
        }
        body {
            margin: 0px;
        }
        #div0 { margin-left: 20px; height: 100px; box-sizing: border-box; }
        #div1 { margin-left: 10px; height: 100px; box-sizing: border-box; }
    </style>
</head>
<body>

<div id=scrolloffsetcontainer>
    <div id=div0>
        <a id=a0 href="#div0">Link 0</a>
    </div>
    <div id=div1>
        <a id=a1 href="#div1">Link 1</a>
    </div>
</div>

<p id='description'></p>
<div id='console'></div>

<script>
    var adjustedNode;
    var adjustedPoint;
    // Set up shortcut access to elements
    var e = {};
    ['scrolloffsetcontainer', 'div0', 'div1', 'a0', 'a1'].forEach(function(a) {
        e[a] = document.getElementById(a);
    });

    function adjustedPointAtTouch(x, y, radius)
    {
        var x = x - radius;
        var y = y - radius;
        var width = radius * 2;
        var height = radius * 2;
        var position = internals.touchPositionAdjustedToBestClickableNode(x, y, width, height, document);
        return position;
    }

    function testDirectTouches()
    {
        testTouchPoint(touchPoint(40, 130, 50), e.a0);
        testTouchPoint(touchPoint(30, 230, 50), e.a1);
    }
    function testAdjustedTouches()
    {
        adjustedPoint = adjustedPointAtTouch(10, 120, 80);
        shouldBeNonNull('adjustedPoint');
        adjustedNode = document.elementFromPoint(adjustedPoint.x, adjustedPoint.y);
        shouldBe('adjustedNode', 'e.a0');

        adjustedPoint = adjustedPointAtTouch(5, 220, 80);
        shouldBeNonNull('adjustedPoint');
        adjustedNode = document.elementFromPoint(adjustedPoint.x, adjustedPoint.y);
        shouldBe('adjustedNode', 'e.a1');
    }

    function runTests()
    {
        if (window.testRunner && window.internals && internals.touchNodeAdjustedToBestClickableNode) {
            description('Test touch adjustment when main frame is scroll offset.');
            window.scrollBy(0,400);
            testDirectTouches();
            testAdjustedTouches();
            e.scrolloffsetcontainer.style.display = 'none';
        }
    }
    runTests();
</script>


</body>
</html>
